/*******************************************************************************
 * 版权：北京润尼尔网络科技有限公司，保留所有版权<br>
 * 版本：v1.0.0<br>
 * 文件：CommandConnect.js<br>
 * 日期：2016年9月20日<br>
 * 作者: 王志远<br>
 * 功能：添加命令<br>
 * 
 * 修改记录：<br>
 * 日期 描述 更新功能<br>
 ******************************************************************************/

window.makeNamespace(window,"Rainier.UI.V2");

/**
 * 类型名称：Rainier.UI.V2.CommandConnect<br>
 * 类型功能：添加元件命令<br>
 */
Rainier.UI.V2.CommandConnect = draw2d.command.Command.extend({
	
	NAME : "Rainier.UI.V2.CommandConnect",
	
	/**
	 * 函数名称：Rainier.UI.V2.CommandConnect.init<br>
	 * 函数功能：构造函数<br>
	 * 异常信息：无<br>
	 */
	init : function(context,sourceFigure,targetFigure){
		this._super();
		this.context 	  	= context;
		this.sourceFigure 	= sourceFigure;
		this.targetFigure 	= targetFigure;
		this.createdConnectionModel = null;
		this.connEditpart	= null;
		this.vertices = null;
		this.segments = null;
		this.searchParts();
	},
	
	/**
	 * 函数名称：Rainier.UI.V2.CommandConnect.setVertices<br>
	 * 函数功能：设置连接顶点<br>
	 * 参数输入：vertices:draw2d.util.ArrayList<br>
	 * 函数返回：无<br>
	 * 异常信息：无<br>
	 */
	setVertices : function(vertices){
		this.vertices = vertices;
	},
	
	/**
	 * 函数名称：Rainier.UI.V2.CommandConnect.setSegments<br>
	 * 函数功能：设置连接线段列表<br>
	 * 参数输入：vertices:draw2d.util.ArrayList<br>
	 * 函数返回：无<br>
	 * 异常信息：无<br>
	 */
	setSegments : function(segments){
		this.segments = segments;
	},
	
	/**
	 * 搜索使用到的EditParts
	 */
	searchParts : function(){
		
		if(!this.context){
			return;
		}
		
	   	var rootEditPart 	= this.context.getRootEditPart();
    	var rootFigure   	= this.context.getRootFigure();
    	var rootModel    	= this.context.getRootModel();
    	var sourcePortFigure= this.sourceFigure;
    	var targetPortFigure= this.targetFigure;
    	var sourcePortPart  = null;
    	var targetPortPart  = null;
    	
    	var comparePortPart = function(index,portpart){
    		var portFigure = portpart.getFigure();
			if(!sourcePortPart && portFigure === sourcePortFigure){
				sourcePortPart = portpart;
			}
			if(!targetPortPart && portFigure === targetPortFigure){
				targetPortPart = portpart;
			}
    		if(sourcePortPart && targetPortPart){
    			return false;
    		}
    	};
    	
    	Rainier.Basic.CompositeObject.recursiveEach(rootEditPart,function(part){    		
    		if(part instanceof Rainier.UI.V2.CompositeEditPart){
    			var model 		= part.getModel();
    			var inputparts 	= part.getInputParts();
    			var outputparts = part.getOutputParts();
    			var hybridparts = part.getHybridParts();
    			var connsparts  = part.getConnectionParts();
    			
    			inputparts.each(comparePortPart);
    			
    			outputparts.each(comparePortPart);
    			
    			hybridparts.each(comparePortPart);
    			
    			connsparts.each(function(i,connpart){
    				Rainier.Basic.CompositeObject.recursiveEach(connpart,function(_part){
    	    			var _hybridparts = _part.getHybridParts();
    	    			_hybridparts.each(comparePortPart);
        	    		if(sourcePortPart && targetPortPart){
        	    			return false;
        	    		}
    				});
    			});

    		}
    		
    		if(sourcePortPart && targetPortPart){
    			return false;
    		}
    	});
    	
    	if(sourcePortPart && targetPortPart){
    	   	this.rootEditPart 			= rootEditPart;
    	   	this.rootFigure   			= rootFigure;
    	   	this.rootModel    			= rootModel;
        	this.sourcePortPart  		= sourcePortPart;
        	this.targetPortPart  		= targetPortPart;
        	this.sourcePortPartParent 	= sourcePortPart.getParent();
        	this.targetPortPartParent 	= targetPortPart.getParent();
    	}
	},
	
    /**
	 * 函数名称：draw2d.command.Command.canExecute<br>
	 * 函数功能：是否能够执行命令<br>
	 * 函数返回：Boolean<br>
	 * 异常信息：无<br>
	 */
    canExecute:function(){
    	
		if(!this.context){
			return false;
		}
		
    	if(this.sourceFigure &&
    		this.targetFigure &&
    		this.rootEditPart &&
    		this.rootFigure &&
    		this.rootModel &&
    		this.sourcePortPart &&
    		this.targetPortPart &&
    		this.sourcePortPartParent &&
    		this.targetPortPartParent){
    		
    		/*
    		//保证源端口和目标端口所在的元件在同一个层次上
    		if(this.sourcePortPartParent.getParent() === this.targetPortPartParent.getParent()){
    			return true;
    		}
    		
    		//保证源端口和目标端口所在的元件在同一个层次上
    		if(this.sourcePortPartParent.getParent() === this.targetPortPartParent){
    			return true;
    		}
    		
    		//保证源端口和目标端口所在的元件在同一个层次上
    		if(this.sourcePortPartParent === this.targetPortPartParent.getParent()){
    			return true;
    		}
    		*/
    		return true;
    	}
    	return false;
    },
    
    /**
	 * 函数名称：draw2d.command.Command.execute<br>
	 * 函数功能：执行命令<br>
	 * 	 {
	 *     		type:Rainier.UI.V2.ConnectionModel,
	 *     		id	:'111-111-111-112',
	 *     		uiProperty:{
	 * 				'outlineStroke'	:3,
	 *   			'outlineColor'	:'#303030',
	 *   			'stroke'		:1,
	 *   			'radius'		:5,
	 *   			'color'			:'#00A8F0'
	 *     		},
	 *     
	 *     		modelProperty:{
	 *     			name	: 'connection1',
	 *     			router	: 'Rainier.UI.V2.InteractiveManhattanRouter',
	 *     			segments:[
	 *     				{start_x:100,start_y:100,end_x:200,end_y:200,type='horizontal'},
	 *     				{start_x:100,start_y:100,end_x:200,end_y:200,type='vertical'},
	 *     				{start_x:100,start_y:100,end_x:200,end_y:200,type='horizontal'},
	 *     			],
	 *     			source_id : 'xxxx-xxxx-xxxx-xxxx',
	 *     			target_id : 'xxxx-xxxx-xxxx-xxxx',
	 *     		},
	 *     	},
	 * 函数返回：无<br>
	 * 异常信息：无<br>
	 */
    execute:function(){
    	if(this.sourceFigure &&
    		this.targetFigure &&
    		this.rootEditPart &&
    		this.rootFigure &&
    		this.rootModel &&
    		this.sourcePortPart &&
    		this.targetPortPart &&
    		this.sourcePortPartParent &&
    		this.targetPortPartParent
    	){
    		
    		var property = {};
    		property.uiProperty={
//    			'outlineStroke'	:1,
//    			'outlineColor'	:'#303030',
    			'stroke'		:1,
    			'radius'		:1,
    			'color'			:'#00A8F0'
    		};
    		
    		property.modelProperty={
    				name	  : 'connection1',
    				router	  : 'Rainier.UI.V2.InteractiveManhattanRouter',
    				segments  : [],
    				source_id : this.sourcePortPart.getModel().getUuid(),
    				target_id : this.targetPortPart.getModel().getUuid(),
    		};
    		
    		this.segments.each(function(i,seg){
    			var _seg = {
    					start_x:seg.start.x,
    					start_y:seg.start.y,
    					end_x:seg.end.x,
    					end_y:seg.end.y,
    					type:seg.type
    			};
    			property.modelProperty.segments.push(_seg);
    		});

    		var connParentPart = this.sourcePortPartParent.getParent();
    		var connParentModel= connParentPart.getModel();
    		if(connParentModel instanceof Rainier.UI.V2.ConnectionModel){
    			connParentModel = connParentModel.getParent();
    		}
    		
    		this.createdConnectionModel = connParentModel.connect(this.sourcePortPart.getModel(),this.targetPortPart.getModel(),property,false);
			// this.context.circuitparser.parse();
    	}
    },
    
    /**
	 * 函数名称：draw2d.command.Command.undo<br>
	 * 函数功能：撤销命令<br>
	 * 函数返回：无<br>
	 * 异常信息：无<br>
	 */
    undo:function(){
    	var connParentPart = null;
    	if(this.sourcePortPartParent instanceof Rainier.UI.V2.DeviceEditPart){
    		connParentPart = this.sourcePortPartParent.getParent();
    	}
    	if(this.sourcePortPartParent instanceof Rainier.UI.V2.JointEditPart){
    		connParentPart = this.sourcePortPartParent.getParent().getParent();
    	}
    	var connParentModel= connParentPart.getModel();
    	connParentModel.removeConnection(this.createdConnectionModel);
    	connParentPart.refreshChildren();    		
    	//
//		this.context.circuitparser.parse();
    },
    
    /**
	 * 函数名称：draw2d.command.Command.redo<br>
	 * 函数功能：重做命令<br>
	 * 函数返回：无<br>
	 * 异常信息：无<br>
	 */
    redo:function(){
    	
    	var conn = this.createdConnectionModel;
    	
    	var connParentPart = null;
    	if(this.sourcePortPartParent instanceof Rainier.UI.V2.DeviceEditPart){
    		connParentPart = this.sourcePortPartParent.getParent();
    	}
    	if(this.sourcePortPartParent instanceof Rainier.UI.V2.JointEditPart){
    		connParentPart = this.sourcePortPartParent.getParent().getParent();
    	}
    	var connParentModel= connParentPart.getModel();
    	
    	var sourceModel = this.sourcePortPart.getModel();
    	var targetModel = this.targetPortPart.getModel();
    	
		conn.attachSourcePort(sourceModel);
		sourceModel.attachOutgoing(conn);
		
		conn.attachTargetPort(targetModel);
		targetModel.attachIncoming(conn);
		
    	connParentModel.addConnection(this.createdConnectionModel);
    	connParentPart.refreshChildren();
//		this.context.circuitparser.parse();
    	this.context.rootModel.setConnectionPortValue(sourceModel,targetModel);
    }
});

